package com.example.bottomnavigationpt;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.bottomnavigationpt.fragment.Fragment1;
import com.example.bottomnavigationpt.fragment.Fragment2;
import com.example.bottomnavigationpt.fragment.Fragment3;
import com.example.bottomnavigationpt.fragment.Fragment4;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private BottomNavigationView mBottomNavigationView;
    private List<Fragment> fragments;
    MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = findViewById(R.id.viewpager);
        mBottomNavigationView = findViewById(R.id.bottomnavigationview);
        fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragments.add(new Fragment4());

        TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(adapter);

        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.person:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.find:
                        mViewPager.setCurrentItem(2);
                        break;
                    case R.id.settings:
                        mViewPager.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem == null) {
                    menuItem = mBottomNavigationView.getMenu().getItem(0);
                }
                //将上次的选择设置为false，等待下一次的选择
                menuItem.setChecked(false);
                menuItem=mBottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class TabsAdapter extends FragmentPagerAdapter {
        List<Fragment> fragments;

        public TabsAdapter(@NonNull FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}